package com.openapi.client;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.openapi.utils.EncodeUtil;
import com.openapi.utils.SignUtil;

import java.util.HashMap;
import java.util.Map;


/**
 * ClassName: OpenClient
 * Package: open.api.client
 * Description:
 *
 * @Author: @weixueshi
 * @Create: 2024/4/26 - 20:53
 * @Version: v1.0
 */
public class OpenClient {

    /**
     * 添加密钥
     */
    private String secretKey;

    private String accessKey;

    /**
     * 服务接口基地址
     */
    private final  String BASE_ULR = "http://localhost:9000/provider";

    public OpenClient(String accessKey, String secretKey) {
        this.accessKey = accessKey;
        this.secretKey = secretKey;
    }

    public String queryUserNameByGet(String username){
        HashMap<String, Object> param = new HashMap<>();
        param.put("username", username);
        param.put("url",BASE_ULR + "/getUserName/byGet");
        String json = JSONUtil.toJsonStr(param);
        return HttpRequest.get(BASE_ULR + "/getUserName/byGet")
                .addHeaders(getHeader(json))
                .form(param)
                .execute()
                .body();
    }

    /**
     * 无请求参数接口
     */
    public String queryInfoNoParamByGet(){
        HashMap<String, Object> param = new HashMap<>();
        param.put("url",BASE_ULR + "/getInfo/byGet/withNoParam");
        String json = JSONUtil.toJsonStr(param);
        return HttpRequest.get(BASE_ULR + "/getInfo/byGet/withNoParam")
                //封装请求头参数
                .addHeaders(getHeader(json))
                .body(json)
                .execute()
                .body();
    }

    public String queryUserNameByPost(String username){
        Map<String,Object> param = new HashMap<>();
        param.put("username", username);
        param.put("url",BASE_ULR + "/getUserName/byPost");
        String json = JSONUtil.toJsonStr(param);
        return HttpRequest.post(BASE_ULR + "/getUserName/byPost")
                //封装请求头参数
                .addHeaders(getHeader(json))
                .form(param)
                .execute()
                .body();
    }

    public String queryUserInfo(String username,String password){
        Map<String,Object> param = new HashMap<>();
        param.put("username", username);
        param.put("password", password);
        param.put("url",BASE_ULR + "/query/user");
        String json = JSONUtil.toJsonStr(param);
        return HttpRequest.post(BASE_ULR + "/query/user")
                //封装请求头参数
                .addHeaders(getHeader(json))
                .form(param)
                .execute()
                .body();
    }

    /**
     * 封装header参数
     * @return
     */
    private Map<String, String> getHeader(String body){
        Map<String, String> header = new HashMap<>();
        header.put("accessKey", this.accessKey);
        //将不在服务器间传递
        //header.put("secretKey", this.secretKey);
        //解决中文乱码问题
        String encodeBody = EncodeUtil.encodeBody(body);
        header.put("body",encodeBody);
        header.put("sign", SignUtil.getSign(encodeBody,secretKey));
        //随机数 + 时间戳的组合实现将在SDK封装时实现。
        header.put("nonce", RandomUtil.randomNumbers(4));
        header.put("timestamp", String.valueOf(System.currentTimeMillis() / 1000));
        return header;
    }
}
